import { message, Modal } from 'antd';
/* eslint no-useless-escape:0 import/prefer-default-export:0 */
const reg =
	/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

export const isUrl = (path: string): boolean => reg.test(path);


// 手机脱敏
export const noPassByMobile = (str: string) => {
	if (str !== null && str !== undefined) {
		const pat = /(\d{3})\d*(\d{4})/;
		return str.replace(pat, '$1****$2');
	} else {
		return "";
	}
}
/**
 * 防抖, 一段时间内只有最后一次响应触发
 * @param func 函数
 * @param waitTime 时间
 */
export function debounce(func, waitTime = 600, isGoLast = false) {
	if (isGoLast) {
		var timeout: any;
		return function () {
			var context = this;
			var args = arguments;
			clearTimeout(timeout);
			timeout = setTimeout(function () {
				func.apply(context, args)
			}, waitTime);
		}
	} else {
		var canRun = true
		return function (...args) {
			if (!canRun) {
				return
			} else {
				canRun = false
				func.apply(this, args) // 将方法放在外面, 这样即便该函数是异步的，也可以保证在下一句之前执行
				setTimeout(function () { canRun = true }, waitTime)
			}
		}
	}

}

// 计算富文本字数
export const getRichTextNum = (val: any) => {
	let num: number = 0;

	if (val) {
		let str = val.replace(/<\/?[^>]*>/g, ''); // 去除HTML tag
		str = str.replace(/[ | ]*\n/g, '\n'); // 去除行尾空白
		str = str.replace(/ /ig, ''); // 去掉 
		num = str.length;
	}
	return num;
}